package com.zhb.core.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @Title: RabbitConfig.java 
 * @Package com.zzh.sys.configuration 
 * @Description: rabbit消息队列配置
 * @author zhb
 * @date 2016年3月6日 下午9:18:16 
 * @version V1.0
 */
@Configuration
public class RabbitConfig {
	@Autowired
	private ConnectionFactory rabbitConnectionFactory;

	// 交换机
	@Bean
	DirectExchange rubeExchange() {
		return new DirectExchange("rmq.rube.exchange", true, false);
	}

	// 消息队列
	@Bean
	public Queue rubeQueue() {
		return new Queue("rmq.rube.queue", true);
	}

	// 将队列绑定到交换机
	@Bean
	Binding rubeExchangeBinding(DirectExchange rubeExchange, Queue rubeQueue) {
		return BindingBuilder.bind(rubeQueue).to(rubeExchange).with("rube.key");
	}

	@Bean
	public RabbitTemplate rubeExchangeTemplate() {
		RabbitTemplate r = new RabbitTemplate(rabbitConnectionFactory);
		r.setExchange("rmq.rube.exchange");
		r.setRoutingKey("rube.key");
		r.setConnectionFactory(rabbitConnectionFactory);
		return r;
	}

	@Bean
	public RabbitMessagingTemplate rabbitMessagingTemplate() {
		return new RabbitMessagingTemplate(rubeExchangeTemplate());
	}
}
